﻿using otis.entities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍

namespace otis
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class PageService : Page
    {
        public PageService()
        {
            this.InitializeComponent();
            //
            this.BindData();
        }

        private void fvSetup_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int i = 0;
            foreach (Image img in this.spPoint.Children)
            {
                if (i == this.fvService.SelectedIndex)
                {
                    img.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:///Assets/common/black.png"));
                }
                else
                {
                    img.Source = new Windows.UI.Xaml.Media.Imaging.BitmapImage(new Uri("ms-appx:///Assets/common/white.png"));
                }
                i++;
            }
        }

        private void BindData()
        {
            var items = entities.ServiceHelper.InitServices();
            this.fvService.ItemsSource = items;
            this.spIndex(items.Count);
        }

        private void spIndex(int imgCount)
        {
            //默认第一个索引按钮是黑色
            List<Service> serviceImages = new List<Service>() {
                new Service(){ Id = 0, Name = "",ImageUrl = "ms-appx:///Assets/common/black.png"
                }};
            //有几张展示图片，就再添加imgCount - 1个灰色小按钮
            for (int i = 1; i < imgCount; i++)
            {
                serviceImages.Add(new Service { Id = i, ImageUrl = "ms-appx:///Assets/common/white.png" });
            }
            for (int i = 0; i < serviceImages.Count; i++)
            {
                Image img = new Image() { Source = new BitmapImage(new Uri(serviceImages[i].ImageUrl)), Width = 16, Height = 16, Margin = new Thickness { Right = 5 } };
                img.Tapped += img_Tapped;
                this.spPoint.Children.Add(img);
            }
        }

        void img_Tapped(object sender, TappedRoutedEventArgs e)
        {
            //设置索引条图像
            foreach (Image img in this.spPoint.Children)
            {
                img.Source = new BitmapImage(new Uri("ms-appx:///Assets/common/white.png"));
            }
            //当前点击的索引项变成黑色圆
            (sender as Image).Source = new BitmapImage(new Uri("ms-appx:///Assets/common/black.png"));
            //设置FilpView索引
            bool myLock = true; //设置一个锁，每次进行以下循环的时候都使index++，当循环到点击的项时加锁，不再进行index++，因此循环结束后index就是FlipView当前应该显示的图片的索引
            int index = 0;
            for (int i = 0; i < this.spPoint.Children.Count; i++)
            {
                if (this.spPoint.Children[i] != sender && myLock)
                {
                    index++;
                }
                else
                {
                    myLock = false;
                }
            }
            this.fvService.SelectedIndex = index; //设置FlipView显示对应的图片
        }

        private void btnHome_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(MainPage));
        }
    }
}
